package com.yantong.controller.centre;

import com.yantong.annotation.Log;
import com.yantong.controller.BaseController;
import com.yantong.result.ResultVo;
import com.yantong.utils.PageUtils;
import com.yantong.pojo.GoodsPic;
import com.yantong.service.GoodsPicService;
import com.yantong.service.ConfigService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * 广告控制类
 *
 * @author kanghaixin
 * @date 2018/8/29.11:22
 */
@Controller
@RequestMapping("/mall/goodspic")
public class GoodsPicController extends BaseController {
    private static final String prefix = "mall/goodspic";

    @Autowired
    private ConfigService configService;

    @Autowired
    private GoodsPicService goodsPicService;

    /**
     * 主题图页面
     * @param model
     * @return
     */
    @RequiresPermissions("mall:goodspic:goodspic")
    @GetMapping()
    public String adverts(Model model) {
        model.addAttribute("imgPrefix", configService.getImgPrefix());
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/goodspic";
    }

    /**
     * 主题图列表数据
     *
     * @param advName
     * @param offset
     * @param limit
     * @return
     */
    @GetMapping("/list")
    @RequiresPermissions("mall:goodspic:goodspic")
    @ResponseBody
    public PageUtils<GoodsPic> listAdverts(String advName, Long centreId, int offset, int limit) {
        if (centreId == null) {
            centreId = getUserCentreId();
        }
        return goodsPicService.listAdverts(advName, centreId,offset, limit);
    }

    /**
     * 主题添加页面
     * @param model
     * @return
     */
    @RequiresPermissions("mall:goodspic:add")
    @GetMapping("/add")
    public String add(Model model) {
        model.addAllAttributes(getCentreIdModelInfo());
        return prefix + "/add";
    }

    /**
     * 主题数据保存
     * @return
     */
    @Log(module = "商品主题管理", operation = "主题数据保存")
    @ResponseBody
    @RequiresPermissions("mall:goodspic:add")
    @PostMapping("/save")
    public ResultVo save(GoodsPic advert) {
        if (advert == null) {
            return ResultVo.error();
        }
        if (goodsPicService.saveAdvert(advert)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 主题编辑
     * @return
     */
    @RequiresPermissions("mall:goodspic:edit")
    @GetMapping("/edit/{advertId}")
    public String edit(@PathVariable Long advertId, Model model) {
        model.addAttribute("advert", goodsPicService.getByKey(advertId));
        return prefix +"/edit";
    }

    /**
     * 主题编辑
     * @param advert
     * @return
     */
    @Log(module = "商品主题管理", operation = "主题编辑")
    @ResponseBody
    @RequiresPermissions("mall:goodspic:edit")
    @PostMapping("/update")
    public ResultVo update(GoodsPic advert) {
        if (advert == null || advert.getId() == null) {
            return ResultVo.error();
        }
        if (goodsPicService.updateAdvert(advert)< 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    /**
     * 删除广告
     * @param advertId
     * @return
     */
    @Log(module = "商品主题管理", operation = "删除广告")
    @RequiresPermissions("mall:goodspic:remove")
    @PostMapping("/remove")
    @ResponseBody
    public ResultVo remove(Integer advertId) {
        if (advertId == null) {
            return ResultVo.error();
        }
        if (goodsPicService.removeAdvert(advertId) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }
}
